package example.telosys.persistence;

import org.telosys.common.dataset.DataRow;
import org.telosys.common.dataset.DataSet;
import org.telosys.persistence.PersistenceManager;
import org.telosys.persistence.PersistenceManagerFactory;
import org.telosys.persistence.TelosysPersistence;
import org.telosys.persistence.sql.SqlQuery;

public class SampleDataSet {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		PersistenceManagerFactory pmf = TelosysPersistence.getPersistenceManagerFactory() ;
		PersistenceManager pm = pmf.createPersistenceManager() ;
		
		// Hard coded SQL request 
		SqlQuery query = new SqlQuery("select name, code from country where name like ? order by name");
		query.setParameter(1, "F%");

		DataSet ds = pm.loadDataSet(query);
		for ( DataRow dr : ds ) {
			System.out.println(dr);
		}
		
		// Named SQL request (SQL file name)
		SqlQuery query2 = pm.getNamedQuery("countries"); // countries.sql
		query2.setParameter(1, "A%");
		int count = pm.countDataSetRows(query2);
		System.out.println( count + " row(s) for this request");
		DataSet ds2 = pm.loadDataSet(query2);

		pm.close();		
	}

}
